package com.crm.backend.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.crm.backend.domain.entity.RestBean;
import com.crm.backend.domain.po.Customer;
import com.crm.backend.domain.po.DevPlan;
import com.crm.backend.domain.po.SaleChance;
import com.crm.backend.service.ICustomerService;
import com.crm.backend.service.IDevPlanService;
import com.crm.backend.service.ISaleChanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/v1/dev-plan")
public class DevPlanController {

    @Autowired
    private IDevPlanService devPlanService;

    @Autowired
    private ISaleChanceService saleChanceService;


    @Autowired
    private ICustomerService customerService;
    /**
     * 新增开发计划
     * @param devPlan 开发计划信息
     * @return RestBean封装的响应结果
     */
    @PostMapping
    public RestBean<Void> addDevPlan(@RequestBody DevPlan devPlan) {
        boolean savedDevPlan = devPlanService.save(devPlan);
        return RestBean.success();
    }

    /**
     * 修改开发计划
     * @param id 开发计划编号
     * @param devPlan 修改后的开发计划信息
     * @return RestBean封装的响应结果
     */
    @PutMapping("/{id}")
    public RestBean<Void>  updateDevPlan(@PathVariable(value = "id") Integer id, @RequestBody DevPlan devPlan) {
        devPlan.setDevId(id);
        boolean updatedDevPlan = devPlanService.updateById(devPlan);
        if (updatedDevPlan) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "开发计划未找到");
        }
    }

    /**
     * 删除开发计划
     * @param id 开发计划编号
     * @return RestBean封装的响应结果
     */
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteDevPlan(@PathVariable(value = "id")  Integer id) {
        boolean deleted = devPlanService.removeById(id);
        if (deleted) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "开发计划未找到");
        }
    }

    /**
     * 执行开发计划
     * @param id 开发计划编号
     * @param executeDate 执行日期
     * @param executeResult 执行结果
     * @return RestBean封装的响应结果
     */
    @PostMapping("/{id}/execute")
    public RestBean<Void>  executeDevPlan(@PathVariable(value = "id") Integer id, @RequestParam(value = "executeDate") LocalDate executeDate, @RequestParam(value = "executeResult") String executeResult) {
        DevPlan devPlan = devPlanService.getById(id);
        if (devPlan != null) {
            devPlan.setDevDoe(executeDate);
            devPlan.setDevEr(executeResult);
            boolean updatedDevPlan = devPlanService.updateById(devPlan);
            return RestBean.success();
        } else {
            return RestBean.failure(404, "开发计划未找到");
        }
    }

    /**
     * 开发成功
     * @param id 销售机会编号
     * @param customerInfo 客户信息
     * @return RestBean封装的响应结果
     */
    @PutMapping("/sale-chance/{id}/success")
    public RestBean<SaleChance> successSaleChance(@PathVariable(value = "id") Integer id, @RequestBody Map<String, Object> customerInfo) {
        // 获取销售机会
        SaleChance saleChance = saleChanceService.getById(id);
        if (saleChance == null) {
            return RestBean.failure(404, "销售机会未找到");
        }

        // 设置销售机会状态为开发成功
        saleChance.setSalState(3);
        saleChanceService.updateById(saleChance);

        // 根据销售机会信息自动创建客户记录
        Customer customer = new Customer();
        // 将 customerInfo 中的值复制到 customer 对象
        BeanUtil.copyProperties(customerInfo, customer);

        // 验证客户信息是否完整
        if (StrUtil.isEmpty(customer.getCusName()) || StrUtil.isEmpty(customer.getCusRegion()) || StrUtil.isEmpty(customer.getCusIndustry())) {
            return RestBean.failure(400, "客户信息不完整");
        }
        // 设置客户状态为正常
        customer.setCusStatus(1);
        customerService.save(customer);

        return RestBean.success();
    }



        /**
     * 开发失败
     * @param id 销售机会编号
     * @return RestBean封装的响应结果
     */
    @PutMapping("/sale-chance/{id}/fail")
    public RestBean<Void>  failSaleChance(@PathVariable(value = "id") Integer id) {
        SaleChance saleChance = saleChanceService.getById(id);
        if (saleChance != null) {
            saleChance.setSalState(4); // 设置状态为开发失败
            saleChanceService.updateById(saleChance);
            return RestBean.success();
        } else {
            return RestBean.failure(404, "销售机会未找到");
        }
    }
}




